const models = require("../db/mysqldb/index");
const { Op } = require("sequelize");

class DataSystemDictDataModel {
  /**
   * @description 通过 dict_label 查找数据，不包含删除数据
   * @param {string} dict_label
   * @returns
   */
  static async findBySystemDictDataName({ dict_label, dict_type }) {
    return await models.sys_dict_data.findOne({
      where: {
        dict_label,
        dict_type,
        del_flag: "0",
      },
    });
  }
  /**
   * @description 通过 dict_value 查找数据，不包含删除数据
   * @param {string} dict_value
   * @param {string} dict_code
   * @returns
   */
  static async findBySystemDictDataValue({ dict_value, dict_type }) {
    return await models.sys_dict_data.findOne({
      where: {
        dict_value,
        dict_type,
        del_flag: "0",
      },
    });
  }
  /**
   * @description 通过 dict_label id 查找数据，不包含删除数据
   * @param {string} dict_label
   * @param {string} dict_type
   * @returns
   */
  static async findBySystemDictDataNameNeId({
    dict_label,
    dict_type,
    dict_code,
  }) {
    return await models.sys_dict_data.findOne({
      where: {
        dict_label,
        del_flag: "0",
        dict_type,
        dict_code: {
          [Op.ne]: dict_code,
        },
      },
    });
  }
  /**
   * @description 通过 dict_value id 查找数据，不包含删除数据
   * @param {string} dict_value
   * @param {string} dict_type
   * @returns
   */
  static async findBySystemDictDataTypeNeCode({
    dict_value,
    dict_type,
    dict_code,
  }) {
    return await models.sys_dict_data.findOne({
      where: {
        dict_value,
        del_flag: "0",
        dict_type,
        dict_code: {
          [Op.ne]: dict_code,
        },
      },
    });
  }
  /**
   * @description 通过 dict_code 查找数据，不包含删除数据
   * @param {string} dict_code
   * @returns
   */
  static async findBySystemdDictDataId({ dict_code }) {
    return await models.sys_dict_data.findOne({
      where: {
        dict_code,
        del_flag: "0",
      },
    });
  }
  /**
   * @description 通过 dict_type 查找数据，不包含删除数据
   * @param {string} dict_type
   * @returns
   */
  static async findBySystemdDictType({ dict_type }) {
    return await models.sys_dict_data.findAll({
      where: {
        dict_type,
        del_flag: "0",
      },
    });
  }
  /**
   * @description create
   * @param {String} dict_value
   * @param {String} dict_label
   * @param {Number} dict_sort
   * @param {String} dict_type
   * @param {String} css_class
   * @param {String} list_class
   * @param {String} is_default
   * @param {String} status
   * @param {String} remark
   * @param {String} op_user_id
   * @returns
   */
  static async create({
    dict_label,
    dict_value,
    dict_sort,
    dict_type,
    css_class,
    list_class,
    is_default,
    status,
    remark,
    op_user_id,
  }) {
    return await models.sys_dict_data.create({
      dict_label,
      dict_value,
      dict_sort,
      dict_type,
      css_class,
      list_class,
      is_default,
      status,
      remark,
      create_by: op_user_id,
      create_time: Date.now(),
    });
  }
  /**
   * @description 更新
   * @param {Number} dict_code
   * @param {String} dict_value
   * @param {String} dict_label
   * @param {Number} dict_sort
   * @param {String} dict_type
   * @param {String} css_class
   * @param {String} list_class
   * @param {String} is_default
   * @param {String} status
   * @param {String} remark
   * @param {String} op_user_id
   * @returns
   */
  static async update({
    dict_code,
    dict_label,
    dict_value,
    dict_sort,
    dict_type,
    css_class,
    list_class,
    is_default,
    status,
    remark,
    op_user_id,
  }) {
    return await models.sys_dict_data.update(
      {
        dict_code,
        dict_label,
        dict_value,
        dict_sort,
        dict_type,
        css_class,
        list_class,
        is_default,
        status,
        remark,
        update_by: op_user_id,
        update_time: Date.now(),
      },
      {
        where: { dict_code },
      }
    );
  }
  /**
   * @description 删除
   * @param {*} op_user_id
   * @param {*} dict_code
   * @returns
   */
  static async delete({ op_user_id, dict_code }) {
    return await models.sys_dict_data.update(
      {
        del_flag: "2",
        update_by: op_user_id,
      },
      {
        where: { dict_code },
      }
    );
  }

  /**
   * @description 批量删除
   * @param {Number} op_user_id
   * @param {Array} ids
   * @returns
   */
  static async deleteByIds({ op_user_id, ids }) {
    return await models.sys_dict_data.update(
      {
        del_flag: "2",
        update_by: op_user_id,
      },
      {
        where: {
          dict_code: {
            [Op.in]: ids,
          },
        },
      }
    );
  }
  /**
   * @description 获取字典信息
   * @param {String} dict_label
   * @param {String} dict_value
   * @param {String} status
   * @param {String} limit
   * @param {String} size
   * @returns
   */
  static async list({
    limit,
    size,
    dict_label,
    dict_value,
    status,
    dict_type,
  }) {
    let querySql = "";
    querySql += `and dict_type = '${dict_type}'`;
    if (dict_label) {
      querySql += ` and dict_label like '%${dict_label}%'`;
    }
    if (dict_value) {
      querySql += ` and dict_value like '%${dict_value}%'`;
    }
    if (status) {
      querySql += ` and status = '${status}'`;
    }
    let sql = `SELECT
        dict_code,
        dict_label,
        dict_value,
        dict_sort,
        dict_type,
        css_class,
        list_class,
        is_default,
        status,
        remark,
        create_time
      FROM
        sys_dict_data WHERE del_flag = '0' ${querySql}
      ORDER BY dict_sort ASC,
      update_time desc,create_time desc limit ${(limit - 1) * size}, ${size}`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    const count = await models.sequelize.query(
      `SELECT count(1) as count FROM sys_dict_data WHERE del_flag = '0' ${querySql};`,
      { replacements: ["active"], type: models.sequelize.QueryTypes.SELECT }
    );
    return {
      result,
      count,
    };
  }
  /**
   * @description 获取所有字典信息
   * @returns
   */
  static async all({ dict_type }) {
    let sql = `SELECT
        dict_code,
        dict_label,
        dict_value,
        dict_sort,
        dict_type,
        css_class,
        list_class,
        is_default,
        status,
        remark
      FROM
        sys_dict_data WHERE del_flag = '0' and dict_type = '${dict_type}'
      ORDER BY dict_sort ASC,
        create_time DESC,
        update_time DESC`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }
  /**
   * @description 获取字典信息
   * @param {String} dict_label
   * @param {String} dict_value
   * @param {String} status
   * @returns
   */
  static async findList({ dict_label, dict_value, status, dict_type }) {
    let querySql = "";
    querySql += `and dict_type = '${dict_type}'`;
    if (dict_label) {
      querySql += ` and dict_label like '%${dict_label}%'`;
    }
    if (dict_value) {
      querySql += ` and dict_value like '%${dict_value}%'`;
    }
    if (status) {
      querySql += ` and status = '${status}'`;
    }
    let sql = `SELECT
        dict_code,
        dict_label,
        dict_value,
        dict_sort,
        dict_type,
        css_class,
        list_class,
        is_default,
        status,
        remark,
        create_time
      FROM
        sys_dict_data WHERE del_flag = '0' ${querySql}
      ORDER BY dict_sort ASC,
      update_time,create_time desc`;
    const result = await models.sequelize.query(sql, {
      replacements: ["active"],
      type: models.sequelize.QueryTypes.SELECT,
    });
    return {
      result,
    };
  }
}
module.exports = {
  DataSystemDictDataModel,
};
